function predicted_class = scsdl_classify(y, D, D_list, Z_tilde_list, lambda, w)
% SCSDL 分类函数
% 输入：
% y              - 测试样本列向量（m x 1）
% D              - 总字典矩阵（m x p），由 D_list 拼接而成
% D_list         - 单元格数组，每类子字典 {D1, D2, ..., DK}
% Z_tilde_list   - 每类训练稀疏系数 {Z1, Z2, ..., ZK}
% lambda         - 稀疏正则项
% w              - 判别项权重

K = length(D_list);
alpha_hat = lasso(D, y, 'Lambda', lambda, 'Standardize', false);

% 拆分 alpha_hat 为每类的子系数 alpha_i，同时记录索引区间
alpha_list = cell(K, 1);
alpha_idx = zeros(K, 2);  % 每类的起止下标
cursor = 1;
for i = 1:K
    Di = D_list{i};
    d_len = size(Di, 2);
    alpha_list{i} = alpha_hat(cursor : cursor + d_len - 1);
    alpha_idx(i, :) = [cursor, cursor + d_len - 1];
    cursor = cursor + d_len;
end

% 计算每一类的 e_i
e_list = zeros(K, 1);
for i = 1:K
    Di = D_list{i};
    alpha_i = alpha_list{i};
    
    % 第一项：重建误差
    recon_error = norm(y - Di * alpha_i, 2)^2;

    % 第二项：与其他类表示系数的相似度（判别项）
    coef_term = 0;
    for j = 1:K
        if j ~= i
            idx_j = alpha_idx(j, 1):alpha_idx(j, 2);
            alpha_j = alpha_hat(idx_j);         % 提取 alpha_j
            Zj = Z_tilde_list{j};               % size: d_j x n_j
            coef_term = coef_term + norm(Zj' * alpha_j, 'fro')^2 / size(Zj, 2);
        end
    end

    % 综合得分
    e_list(i) = recon_error + w * coef_term;
end

% 选择得分最小的类
[~, predicted_class] = min(e_list);
end
